import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Query, Req, SetMetadata } from "@nestjs/common";
import { UserService } from "./user.service";
import { CreateUserDto } from "./dto/create-user.dto";
import { UpdateUserDto } from "./dto/update-user.dto";
import { LoginDto } from "./dto/login-dto";
import { AuthGuard } from "src/auth/auth.guard";
import { User } from "./entities/user.entity";
import { Request } from 'express';
import { RolesGuard } from "src/auth/roles.guard";


@Controller("/mg/user")
export class UserController {
    constructor(private readonly userService: UserService) { }

    @UseGuards(AuthGuard, RolesGuard) 
    @SetMetadata("roles", ["超级管理员"])
    @Post()
    create(@Body() createUserDto: CreateUserDto) {
        console.log("createUserDto", createUserDto);
        return this.userService.create(createUserDto);
    }

    @UseGuards(AuthGuard, RolesGuard) 
    @SetMetadata("roles", ["超级管理员"])
    @Get()
    findAll() {
        return this.userService.findAll();
    }

    @UseGuards(AuthGuard)
    @Get("info")
    getInfo(@Req() request: Request) {
        const user: User = request.user as any;
        console.log(request)
        const { password, ...info } = user;
        return info;
    }

    @Post("login")
    login(@Body() loginDto: LoginDto) {
        return this.userService.login(loginDto);
    }

    @Get('testLogin')
    testLogin(@Query('username') username: string, @Query('password') password: string) {
        return this.userService.testLogin(username, password);
    }

    @Get('testInfo')
    testInfo(@Query('token') token: string) {
        return this.userService.testInfo(token);
    }

    @Get(":id")
    findOne(@Param("id") id: string) {
        return this.userService.findOne(id);
    }

    @Get("permission/:id")
    findOnepermission(@Param("id") id: string) {
        return this.userService.getPermissionsByUserId(id);
    }

    @UseGuards(AuthGuard, RolesGuard) 
    @SetMetadata("roles", ["超级管理员"])
    @Patch(":id")
    update(@Param("id") id: string, @Body() updateUserDto: UpdateUserDto) {
        return this.userService.update(id, updateUserDto);
    }

    @Delete(":id")
    remove(@Param("id") id: string) {
        return this.userService.remove(id);
    }
}
